<template>
  <div class="index">
    <j-editor v-model="model.article.cms_article_data[0].content" @onsave="onsave"/>
  </div>
</template>

<script>
import JEditor from './JEditor.vue'
import {service as http } from '@/api/manage'
import {getQueryString} from "../api/manage";

export default {
  name: 'index',
  components: {
    JEditor
  },
  data () {
    return {
      model:{
        article: {
          // create_by: "admin",//此字段为默认值字段由系统自动填充
          create_by_id: "",//1526481737784188929
          // create_time: "2022-05-25 19:50:05",//此字段为默认值字段由系统自动填充
          /* category_code: "A01A06A04",
          category_id: "1519512270634205185", //TODO 栏目默认随机选
          category_name: "我要吐槽", //cny 额外冗余,通过额外请求获得 */
          category_code: "",
          category_id: "", //TODO 栏目默认随机选
          category_name: "", //cny 额外冗余,通过额外请求获得
          // cms_article_data:[],
           cms_article_data: [{
            cms_article_id: "", //1516762793376714787
            content: "",
            // id: "1516762799093551111",
            id: "", //如果没有id则是新增
            is_can_comment: "N",
            jeecg_row_key: "1516762799093551111",
            relation: ""
          }],
          cms_article_posid: [{
            article_id: "", //1516762793376714787
            // create_by: "", //admin
            // create_time: "", //2022-05-25 18:54:39
            // id: "1520024684958232577",
            id: "", //如果没有id则是新增
            jeecg_row_key: "", //1520024684958232577
            postid: "",
            // update_by: "",
            // update_time: ""
          }],
          color: "",
          copyfrom: "",
          description: "",
          hits: "",
          hits_minus: "",
          hits_plus: "",
          href: "",
          // id: "1516762793376714787",//如果没有id则是新增
          id: "", //如果没有id则是新增
          image: "",
          // image: "https://configservercdn-adm.cqttech.com/3/b/a/3b5af1c5c3896e1b257bf61b59db0b86.png", //online\\image6685470066.PNG//TODO 没有则设置随机默认图
          keywords: "",
          module_type: "1", //默认设置为1 文章
          org_code: "A01A01", //默认当前用户所属的上级区域，即翔安地铁社区
          org_code_name: '翔安地铁社区', //冗余字段,通过额外请求获得
          org_code_id: '26e6bb9d4116453d8f092cbd2f31ebd0', //冗余字段
          remarks: "",
          source: "1",
          status: "",
          sys_org_code: "", //TODO 看是否需要初始化当前用户的部门
          title: "",
          weight: "",
          weight_date: "",
          word_count: ""
        },

      },
    }
  },
  created() {
    var that = this;
    var id =getQueryString('id');

    //初始化 加载了article主附表聚合信息
    if (id) {
      http.get('/online/cgform/api/form/10ff95d27c714aa1a1e69ceba00a0601/' + id + '?_t=' + new Date().getTime())
          .then(res => {
            debugger;
            //当文章是新增，并且进入到富文本编辑器则res.result.cms_article_data=[],需要额外处理
            if(res.result.cms_article_data.length==0||!res.result.cms_article_data[0].content){
              res.result.cms_article_data=[{
                cms_article_id: "", //1516762793376714787
                content: "",
                // id: "1516762799093551111",
                id: "", //如果没有id则是新增
                is_can_comment: "N",
                jeecg_row_key: "1516762799093551111",
                relation: ""
              }]
            }
            that.model.article = res.result;
            return that.model.article;
          }, e => {

          }).then(article => {
        return article;
      })

      // http://39.108.237.61:8080/jeecg-boot/sys/dict/loadDictItem/cms_category,category_name,id?_t=1654049971&key=1519512270634205185
      // http://39.108.237.61:8080/jeecg-boot/online/cgform/api/subform/cms_article_posid/1531538076482904066?_t=1654049971
      // http://39.108.237.61:8080/jeecg-boot/online/cgform/api/subform/cms_article_data/1531538076482904066?_t=1654049971
      // http://39.108.237.61:8080/jeecg-boot/sys/sysDepart/queryTreeList?_t=1654049970
      // http://39.108.237.61:8080/jeecg-boot/sys/dict/loadTreeData?_t=1654049970&pid=0&tableName=cms_category&text=category_name&code=id&pidField=pid&hasChildField=has_child&condition=
      // http://39.108.237.61:8080/jeecg-boot/online/cgform/api/form/10ff95d27c714aa1a1e69ceba00a0601/1531538076482904066?_t=1654049970
    }
  },
  methods: {
    // 小程序检测文本敏感词汇
    secCheckMsg(content) {
      return new Promise((resolve,reject) => {
        debugger;
        /*var header ={...http.config.header};
        header['content-type']='application/x-www-form-urlencoded';//微信小程序要求的content-type*/
        // http.post('cms/cmsArticle/secCheckMsg',{content:content},{header}).then(res=>{
        var httpObj=http;
        http({
          url: 'cms/cmsArticle/secCheckMsg',
          data: JSON.stringify({content:content}),
          method:'post',
          headers: {
            // 'content-type': 'application/x-www-form-urlencoded',
            'content-type': 'application/json',

          }
        }).then(res=>{
          debugger;
          resolve({data:res})
        }).catch(e => {
          reject({data:false})
        })
       /* debugger;
          http.post('cms/cmsArticle/secCheckMsg',{content:content}).then(res=>{
            debugger;
          resolve(res)
        }).catch(e => {
          reject(e)
        })*/


        /* setTimeout(function(){
          if(content=='毛泽东'){
            resolve({data:false})
          }else{
            resolve({data:true})
          }
        },1000) */
      })
    },
    onsave(data){
      var that=this;
      if(that.model.article.cms_article_data[0].content){
        var checkStr=this.getSimpleText(this.model.article.cms_article_data[0].content);
        that.secCheckMsg(checkStr).then(checkResult=>{
          if(checkResult.data){
            //进入该编辑器，一定是uniapp顶层article主表已保存了
            http.put('online/cgform/api/form/10ff95d27c714aa1a1e69ceba00a0601?tabletype=1', this.model.article).then(res => {
              if(res.success){
                uni.postMessage({
                  // data: that.model.article.cms_article_data[0].content
                  data: that.model.article
                });
                //回退后，uniapp通过onShow事件刷新
                uni.navigateBack({
                  delta: 1
                });
                //使用跳转回出现uniapp回退按钮回退回webview问题，所以弃用
                /*uni.navigateTo({
                  // url: '/subpkg/pages/article-edit/article-edit?id=1744698714125185025'
                  url: '/subpkg/pages/article-edit/article-edit?id='+this.model.article.id
                });*/
              }else{
                alert(res.message);
              }

            }, e => {
              alert('网络繁忙');
            });
          }else{
            alert('输入内容包含敏感词！');
          }
        });

      }


    },
    /**
     * 获取不带html标签的简单文本
     * 用于做概要显示，如“富文本富文本....”
     * @param html
     * @returns {*}
     */
    getSimpleText(html){
      //匹配html标签的正则表达式，"g"是搜索匹配多个符合的内容
      var re1 = new RegExp("<.+?>","g")
      //执行替换成空字符
      var msg = html.replace(re1,'')
      return msg;
    }
  }

}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
